{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有数据字典\n",
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "\n",
    "# 空字典\n",
    "dict2 = {}\n",
    "\n",
    "dict3 = dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Tom', 'age': 20, 'gender': '男'} {} {}\n"
     ]
    }
   ],
   "source": [
    "print(dict1,dict2,dict3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Rose', 'age': 20, 'gender': '男'}\n",
      "{'name': 'Rose', 'age': 20, 'gender': '男', 'id': 110}\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "\n",
    "dict1['name'] = 'Rose'\n",
    "# 结果：{'name': 'Rose', 'age': 20, 'gender': '男'}\n",
    "print(dict1)\n",
    "\n",
    "dict1['id'] = 110\n",
    "\n",
    "# {'name': 'Rose', 'age': 20, 'gender': '男', 'id': 110}\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Tom', 'age': 20}\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "\n",
    "del dict1['gender']\n",
    "# 结果：{'name': 'Tom', 'age': 20}\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "del dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'dict1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-e36219336d90>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdict1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'dict1' is not defined"
     ]
    }
   ],
   "source": [
    "dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "\n",
    "dict1.clear()\n",
    "print(dict1)  # {}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "print(dict1['name'])  # Tom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'id'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-b10ab8d1f9aa>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdict1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'id'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'id'"
     ]
    }
   ],
   "source": [
    "print(dict1['id'])  # 报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n",
      "110\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "print(dict1.get('name'))  # Tom\n",
    "print(dict1.get('id', 110))  # 110\n",
    "print(dict1.get('id'))  # None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['name', 'age', 'gender'])\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "print(dict1.keys())  # dict_keys(['name', 'age', 'gender'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Tom', 20, '男'])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "dict1.values()  # dict_values(['Tom', 20, '男']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('name', 'Tom'), ('age', 20), ('gender', '男')])\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "print(dict1.items())  # dict_items([('name', 'Tom'), ('age', 20), ('gender', '男')])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 循环遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "age\n",
      "gender\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "for key in dict1.keys():\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n",
      "20\n",
      "男\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "for value in dict1.values():\n",
    "    print(value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('name', 'Tom')\n",
      "('age', 20)\n",
      "('gender', '男')\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "for item in dict1.items():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = Tom\n",
      "age = 20\n",
      "gender = 男\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "for key, value in dict1.items():\n",
    "    print(f'{key} = {value}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = Tom\n",
      "age = 20\n",
      "gender = 男\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}\n",
    "for (key, value) in dict1.items():\n",
    "    print(f'{key} = {value}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
